Java All-in-One For Dummies by Doug Lowe
Author:Doug Lowe
Language: eng
Format: epub, pdf
Publisher: For Dummies
Published: 2011-07-31T16:00:00+00:00
The rest of this chapter shows you how to use these constructors and methods to work with ArrayList objects.
Creating an ArrayList Object
To create an array list, you first declare an ArrayList variable and then call the ArrayList constructor to instantiate an ArrayList object and assign it to the variable. You can do this on separate lines:
ArrayList signs;
signs = new ArrayList();
Alternatively, you can do it on a single line:
ArrayList signs = new ArrayList();
Here are a few things to note about creating array lists:
✦ The ArrayList class is in the java.util package, so your program must import either java.util.ArrayList or java.util.*.
• Unlike an array, an array list doesn’t make you specify a capacity — though you can if you want. Here’s a statement that creates an array list with an initial capacity of 100:
ArrayList signs = new ArrayList(100);
If you don’t specify a capacity for the array list, the initial capacity is set to 10. Providing at least a rough estimate of how many elements each array list can hold when you create it is a good idea.
✦ The capacity of an array list is not a fixed limit. The ArrayList class automatically increases the list’s capacity whenever necessary.
✦ If you’re using Java 1.5 or later, you can also specify the type of elements the array list is allowed to contain. This statement creates an array list that holds String objects:
ArrayList<String> signs = new ArrayList<String>();
The advantage of specifying a type when you declare an array list is that the compiler complains if you then try to add an object of the wrong type to the list. (This feature is called generics because it lets the Java API designers create generic collection classes that can be used to store any type of object. For more information, refer to Book IV, Chapter 5.)
• The ArrayList class also has a constructor that lets you specify another collection object (typically, another array list) whose items are copied into the new array list. This provides an easy way to make a copy of an array list, but you can also use it to convert any other type of collection to an array list.
Adding Elements
After you create an array list, you can use the add method to add objects to the array list. Here’s code that adds strings to an array list:
signs.add(“Drink Pepsi”);
signs.add(“No minors allowed”);
signs.add(“Say Pepsi, Please”);
signs.add(“7-Up: You Like It, It Likes You”);
signs.add(“Dr. Pepper 10, 2, 4”);
If you specified a type when you created the array list, the objects you add via the add method must be of the correct type.
You can insert an object at a specific position in the list by listing the position in the add method. Consider these statements:
ArrayList<String> nums = new ArrayList<String>();
nums.add(“One”);
nums.add(“Two”);
nums.add(“Three”);
nums.add(“Four”);
nums.add(2, “Two and a half”);
After these statements execute, the nums array list contains the following strings:
One
Two
Two and a half
Three
Four
Here are some important points to keep in mind when you add elements to array lists:
✦ If an array list is already at its capacity when you add an element, the array list automatically expands its capacity.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Beginner's Guides | Reference |
Servlets |
Hello! Python by Anthony Briggs(9867)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9757)
The Mikado Method by Ola Ellnestam Daniel Brolund(9747)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8258)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7745)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7741)
Grails in Action by Glen Smith Peter Ledbrook(7667)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7517)
Windows APT Warfare by Sheng-Hao Ma(6503)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6378)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6249)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6120)
Kotlin in Action by Dmitry Jemerov(5019)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4297)
Functional Programming in JavaScript by Mantyla Dan(4018)
Solidity Programming Essentials by Ritesh Modi(3839)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3614)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3566)
The Ultimate iOS Interview Playbook by Avi Tsadok(3532)
